package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TbGood;
import com.ruoyi.system.mapper.TbShopMapper;
import com.ruoyi.system.domain.TbShop;
import com.ruoyi.system.service.ITbShopService;

/**
 * 商店管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-11
 */
@Service
public class TbShopServiceImpl implements ITbShopService 
{
    @Autowired
    private TbShopMapper tbShopMapper;

    /**
     * 查询商店管理
     * 
     * @param id 商店管理主键
     * @return 商店管理
     */
    @Override
    public TbShop selectTbShopById(Long id)
    {
        return tbShopMapper.selectTbShopById(id);
    }

    /**
     * 查询商店管理列表
     * 
     * @param tbShop 商店管理
     * @return 商店管理
     */
    @Override
    public List<TbShop> selectTbShopList(TbShop tbShop)
    {
        return tbShopMapper.selectTbShopList(tbShop);
    }

    /**
     * 新增商店管理
     * 
     * @param tbShop 商店管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbShop(TbShop tbShop)
    {
        tbShop.setCreateTime(DateUtils.getNowDate());
        // 只保留images字段的第一个url
        if (tbShop.getImages() != null && tbShop.getImages().contains(",")) {
            tbShop.setImages(tbShop.getImages().split(",")[0]);
        }
        int rows = tbShopMapper.insertTbShop(tbShop);
        insertTbGood(tbShop);
        return rows;
    }

    /**
     * 修改商店管理
     * 
     * @param tbShop 商店管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbShop(TbShop tbShop)
    {
        tbShop.setUpdateTime(DateUtils.getNowDate());
        // 只保留images字段的第一个url
        if (tbShop.getImages() != null && tbShop.getImages().contains(",")) {
            tbShop.setImages(tbShop.getImages().split(",")[0]);
        }
        tbShopMapper.deleteTbGoodByShopId(tbShop.getId());
        insertTbGood(tbShop);
        return tbShopMapper.updateTbShop(tbShop);
    }

    /**
     * 批量删除商店管理
     * 
     * @param ids 需要删除的商店管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbShopByIds(Long[] ids)
    {
        tbShopMapper.deleteTbGoodByShopIds(ids);
        return tbShopMapper.deleteTbShopByIds(ids);
    }

    /**
     * 删除商店管理信息
     * 
     * @param id 商店管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbShopById(Long id)
    {
        tbShopMapper.deleteTbGoodByShopId(id);
        return tbShopMapper.deleteTbShopById(id);
    }

    /**
     * 新增商品管理信息
     * 
     * @param tbShop 商店管理对象
     */
    public void insertTbGood(TbShop tbShop)
    {
        List<TbGood> tbGoodList = tbShop.getTbGoodList();
        Long id = tbShop.getId();
        if (StringUtils.isNotNull(tbGoodList))
        {
            List<TbGood> list = new ArrayList<TbGood>();
            for (TbGood tbGood : tbGoodList)
            {
                tbGood.setShopId(id);
                list.add(tbGood);
            }
            if (list.size() > 0)
            {
                tbShopMapper.batchTbGood(list);
            }
        }
    }
}
